package jun.learn.zip;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import jun.work.io.FileZipUtils;

public class Test {
	public static void main(String[] args) throws IOException {
//		pathTest();
//		zipTest();
		useUtil();
	}

	private static void zipTest() throws IOException {
		File f1 = new File("./src/main/java/jun/learn/zip/");
		File[] fs = f1.listFiles();
		ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("./src/main/java/jun/learn/zip/zip.zip"));
		byte[] buff = new byte[1024];
		for(File f : fs){
			FileInputStream fis = new FileInputStream(f);
			BufferedInputStream bis = new BufferedInputStream(fis);
			ZipEntry entry = new ZipEntry(f.getName());
			zos.putNextEntry(entry);
			int count = 0;
			while((count = bis.read(buff)) != -1){
				zos.write(buff, 0, count);
			}
			bis.close();
		}
		zos.close();
	}

	
	public static void useUtil(){
		File f1 = new File("./src/main/java/jun/learn/zip/");
		File[] fs = f1.listFiles();
		File dest = new File("./src/main/java/jun/learn/zip/zip.zip");
		FileZipUtils.compressFiles(fs, dest);
	}
	
	private static void pathTest() throws IOException, FileNotFoundException {
		ClassLoader cl = Thread.currentThread().getContextClassLoader();
		InputStream is = cl.getResourceAsStream("applicationContext.xml");
		System.out.println(is);
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		System.out.println(br.readLine());
		
		InputStream is1 = cl.getResourceAsStream("jun/learn/zip/1.txt");
		System.out.println(is1);
		
		File f = new File("./src/main/java/jun/learn/zip/1.txt");
		InputStream is2 = new FileInputStream(f);
		System.out.println(is2);
		
		File f1 = new File("./src/main/java/jun/learn/zip/");
		File f2 = new File(f1, "test");
		PrintWriter pw = new PrintWriter(f2);
		pw.print("test");
		pw.flush();
		pw.close();
	}
}
